﻿using Autofac.Extras.DynamicProxy;
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Truron.Purchase.Interface;
using Truron.Purchase.Model;
using Truron.Purchase.Model.RemoteModel;
using Truron.Purchase.Service.DbContext;
using Truron.Purchase.Utility;
using Truron.Purchase.Utility.RedisHelper;

namespace Truron.Purchase.Service
{
    [Intercept(typeof(CustomAutofacAop))]
    public class Purchase_OuterRequestApprovalService: IPurchase_OuterRequestApprovalService
    {
        private List<Purchase_OuterRequestApproval> Init(bool isInit = false)
        {
            try
            {
                if (isInit)//初始化
                    RedisBase.Hash_Remove(StaticConstraint.PurchaseOuterRequestApproveCacheKey);
                var List = RedisBase.Hash_GetAll<Purchase_OuterRequestApproval>(StaticConstraint.PurchaseOuterRequestApproveCacheKey);
                if (List != null && List.Count > 0)
                    return List;
                string sql = string.Format(@" select * from Purchase_OuterRequestApproval
                                                            where a.Status!={0} ", (int)Status.delete);
                List = DapperDbContext.Query<Purchase_OuterRequestApproval>(sql).ToList();
                if (List.Count > 0)
                {
                    foreach (var item in List)
                        RedisBase.Hash_Set(StaticConstraint.PurchaseOuterRequestApproveCacheKey, item.Id, item);
                    RedisBase.Item_SetExpire(StaticConstraint.PurchaseOuterRequestApproveCacheKey, DateTime.Now.AddDays(7));
                }
                return List;
            }
            catch (Exception ex)
            {
                LogHelper.Error($"初始化单据数据失败,isInit:{isInit},详细:{ex.ToString()}");
            }
            return new List<Purchase_OuterRequestApproval>();
        }
        public List<Purchase_OuterRequestApproval> GetApproveList(int RequestId)
        {
            return Init().FindAll(x => x.RequestId == RequestId).OrderByDescending(x=>x.CreateTime).ToList();
        }
        public int Save(Purchase_OuterRequestApproval ent, UserCache user)
        {
            IDbModelService<Purchase_OuterRequestApproval> db = new DbModelService<Purchase_OuterRequestApproval>();
            IDbTransaction transaction = null;
            try
            {
                using (var conn = DapperHelper.Instance.OpenCurrentDbConnection())
                {
                    transaction = conn.BeginTransaction();
                    if (ent.Id == 0)
                    {
                        ent.Create(user);
                        ent.Approver = user.UserId;
                        ent.ApproverName = user.RealName;
                        ent.Sort = 0;
                        var noteid = db.InsertOne(ent, conn, transaction);
                        ent.Id = noteid;
                        if(ent.Action==1)
                        {
                            string sql = string.Format(" update Purchase_OuterRequest set FlowStatus={0} where Id={1} ", (int)OuterRequestStatus.agree,ent.RequestId);
                            conn.Execute(sql, null,transaction);
                            RedisBase.Hash_Remove(StaticConstraint.PurchaseOuterRequestCacheKey);
                        }
                        else if(ent.Action==2)
                        {
                            string sql = string.Format(" update Purchase_OuterRequest set FlowStatus={0} where Id={1} ", (int)OuterRequestStatus.reject, ent.RequestId);
                            conn.Execute(sql, null, transaction);
                            RedisBase.Hash_Remove(StaticConstraint.PurchaseOuterRequestCacheKey);
                        }
                        RedisBase.Hash_Set(StaticConstraint.PurchaseOuterRequestApproveCacheKey, noteid, ent);
                        transaction.Commit();
                        return noteid;
                    }
                    else
                    {
                        ent.Modify(user);
                        db.UpdateById(ent, conn, transaction);
                        transaction.Commit();
                        if (ent != null)//更新线索缓存
                        {

                            RedisBase.Hash_Remove(StaticConstraint.PurchaseOuterRequestApproveCacheKey, ent.Id.ToString());
                            RedisBase.Hash_Set(StaticConstraint.PurchaseOuterRequestApproveCacheKey, ent.Id, ent);
                        }
                        return ent.Id;
                    }

                }
            }
            catch (Exception ex)
            {

                transaction.Rollback();
                return -1;
            }
        }
    }
}
